package com.ruoyi.web.controller.system.indexweb;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.IndexUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.IndexTitle;
import com.ruoyi.system.service.IIndexTitleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网站栏目Controller
 * 
 * @author ruoyi
 * @date 2021-07-24
 */
@Controller
@RequestMapping("/system/title")
public class IndexTitleController extends BaseController
{
    private String prefix = "system/title";

    @Autowired
    private IIndexTitleService indexTitleService;

    /**
     * 查询网站栏目列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IndexTitle indexTitle)
    {
        startPage();
        List<IndexTitle> list = indexTitleService.selectIndexTitleList(indexTitle);
        return getDataTable(list);
    }

    /**
     * 导出网站栏目列表
     */
    @Log(title = "网站栏目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IndexTitle indexTitle)
    {
        List<IndexTitle> list = indexTitleService.selectIndexTitleList(indexTitle);
        ExcelUtil<IndexTitle> util = new ExcelUtil<IndexTitle>(IndexTitle.class);
        return util.exportExcel(list, "网站栏目数据");
    }

    /**
     * 新增保存网站栏目
     */
    @Log(title = "网站栏目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IndexTitle indexTitle)
    {
        indexTitle.setCreateTime(new Date(System.currentTimeMillis()));
        indexTitle.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(indexTitleService.insertIndexTitle(indexTitle));
    }

    /**
     * 修改保存网站栏目
     */
    @Log(title = "网站栏目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IndexTitle indexTitle)
    {
        indexTitle.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(indexTitleService.updateIndexTitle(indexTitle));
    }

    /**
     * 修改状态
     */
    @PostMapping("/editState")
    @ResponseBody
    public AjaxResult editState(IndexTitle indexTitle)
    {
        indexTitle = indexTitleService.selectIndexTitleById(indexTitle.getId());
        if (!UtilTool.isNull(indexTitle)){
            int state = indexTitle.getState();
            if (state==0){
                indexTitle.setState(1);
            }else {
                indexTitle.setState(0);
            }
            indexTitle.setUpdateTime(new Date(System.currentTimeMillis()));
            return toAjax(indexTitleService.updateIndexTitle(indexTitle));
        }else {
            return AjaxResult.error("id所属数据不存在");
        }

    }

    /**
     * 删除网站栏目
     */
    @Log(title = "网站栏目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(indexTitleService.deleteIndexTitleByIds(ids));
    }
}
